package com.example.ah_geen_pda.ui.chipfrontsectionin

import android.os.Bundle
import android.widget.CompoundButton
import com.example.ah_geen_pda.R
import com.example.ah_geen_pda.BR
import com.example.ah_geen_pda.base.BaseActivity
import com.example.ah_geen_pda.databinding.ActivityChipfrontsectioninBinding
import com.example.ah_geen_pda.entity.ToastMessageBean
import kotlinx.android.synthetic.main.activity_chipfrontsectionin.*
import kotlinx.android.synthetic.main.activity_title.*

class ChipFrontSectionInAcitvity :BaseActivity<ChipFrontSectionInViewModel,ActivityChipfrontsectioninBinding>(){
    override fun layoutId(): Int {
        return R.layout.activity_chipfrontsectionin
    }

    override fun initVariableId(): Int {
        return BR.chipFrontSectionInViewModel
    }

    override fun initView(savedInstanceState: Bundle?) {
        tvTitle.text = "芯片前段CheckIn"

        initEdit(etLotID,object : EditTextListener{
            override fun onTextChangeListener(s: String) {
                viewModel.getLotInfo(s,false)
            }

        })

        initEdit(etLotCode,object : EditTextListener{
            override fun onTextChangeListener(s: String) {
                viewModel.getLotCodeInfo(s)
            }

        })

        btnTrackIn.setOnClickListener{
            viewModel.trackIn(etStepName.text.toString(),etPart.text.toString())
        }

        allCheck.setOnCheckedChangeListener { _, isChecked ->
            if (viewModel.lotItems.isEmpty()){
                viewModel.defUI.toastEvent.value = ToastMessageBean("请先添加批次数据",false)
                allCheck.isChecked = false
                return@setOnCheckedChangeListener
            }

            viewModel.checkChange(isChecked)
        }

        allTechnicalListCheck.setOnCheckedChangeListener { _, isChecked ->
            if (viewModel.componentItems.isEmpty()){
                viewModel.defUI.toastEvent.value = ToastMessageBean("请先添加批片数据",false)
                allTechnicalListCheck.isChecked = false
                return@setOnCheckedChangeListener
            }

            viewModel.checkTechnicalListChange(isChecked)
        }
    }

    override fun initData() {
        viewModel.defUI.startActivity.observe(this){finish()}

        initEdit(etDurable,object : EditTextListener{
            override fun onTextChangeListener(s: String) {
                viewModel.lotItems.forEach {
                    if (it.LOT.DURABLEID == s){
                        viewModel.defUI.toastEvent.value = ToastMessageBean("当前载具已添加",false)
                        return
                    }
                }
                viewModel.getLotInfo(s,true)
            }

        })
    }
}